﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public partial class User
    {
        public string confirmPassword { get; set; }
        public User getUserById(User user)
        {
            ClubEntities club = new ClubEntities();
            var userQuery = from o in club.Users
                            where o.Name == user.Name
                            select o;
            if (userQuery.Count() == 0)
                throw new Exception("Invalid userid!");
            return userQuery.First<User>();
        }
        public bool Save()
        {
            try
            {
                 ClubEntities club = new ClubEntities();
                var userQuery = from o in club.Users
                                where o.Id == this.Id
                                select o;
                userQuery.First<User>().Password = this.confirmPassword;
                club.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }
        public User getUserById(int id)
        {
            ClubEntities club = new ClubEntities();
            var userQuery = from o in club.Users
                            where o.Id == (short)id
                            select o;
            return userQuery.First<User>();
        }
        public void registerMember(User user)
        {
            ClubEntities club = new ClubEntities();
            club.Users.AddObject(user);

            if (club.Users.Where(c => c.Name == user.Name).Count() > 0)
            {
                throw new Exception("User ID already exist!");
            }

            club.SaveChanges();
        }
        public List<User> getUsers()
        {
            ClubEntities club = new ClubEntities();
            return club.Users.ToList();
        }
        public int getUserCount()
        {
            return new ClubEntities().Users.Count();
        }
        public bool resetPassword(int id)
        {
            ClubEntities club = new ClubEntities();
            try
            {
                var userQuery = from o in club.Users
                            where o.Id == (short)id
                            select o;

                DAL.User l_user = userQuery.First<User>();
                Random l_rnd = new Random();


                string encrypted = Util.Common.CommonUtility.Encrypt(l_rnd.Next(0000000, 9999999).ToString());
                l_user.Password = encrypted;

                club.SaveChanges();
                if (Util.Common.CommonUtility.SendEmail(l_user.Email, "Reset Password", Util.Common.CommonUtility.Decrypt(l_user.Password)))
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        public DateTime? UpdateLogin(short aID, string aRemoteAddr)
        {
            ClubEntities l_entity = new ClubEntities();
            System.Data.Objects.ObjectParameter l_param = new System.Data.Objects.ObjectParameter("logoutTime", typeof(DateTime?));
            l_entity.SP_Update_User_Login(aID, aRemoteAddr, l_param);
            return l_param.Value is DBNull? null: (DateTime?)l_param.Value;
        }

        public void UpdateLogout(short id)
        {
            new ClubEntities().SP_Update_User_Logout(id);
        }
    }
}
